<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>密码策略信息列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">
    <link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>
    <link rel="stylesheet" href="${ctx}/css/public.css">
    <script type="text/javascript">
        var __ctx = "${ctx}";
    </script>
    <link rel="stylesheet" href="${ctx}/css/form.css">
</head>
<body>

<form class="layui-form"  lay-filter="formData" style="background: #fff;height: 100%;padding-top:10px;">
    <div class="addEdit">
    <div class="layui-form-item">
        <label class="layui-form-label">名称<span style="color:red;">*</span></label>
        <div class="layui-input-block" style="width: 70%;margin-left:114px;">
            <input type="text" name="name" lay-verify="required" autocomplete="off" maxlength="32" placeholder="请输入名称" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">初始化方法</label>
        <div class="layui-input-block" style="width: 70%;margin-left:114px;">
            <input type="text" name="initMethod" placeholder="请输入初始化方法" autocomplete="off" maxlength="32" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否系统默认</label>
        <div class="layui-input-block" style="width: 70%;margin-left:114px;">
            <select name="system" lay-filter="aihao">
                <option value=0>否</option>
                <option value=1>是</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">关闭方法</label>
        <div class="layui-input-block" style="width: 70%;margin-left:114px;">
            <input type="text" name="closeMethod" placeholder="请输入关闭方法" style="float: left;" autocomplete="off" maxlength="32" class="layui-input">
            <i class="layui-icon ItemsTips" style="margin-right: -33px;right: 50%;padding-left: 13px;padding-top: 10px;color: #000;display: inline-block;"></i>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">类路径<span style="color:red;">*</span></label>
        <div class="layui-input-inline" style="width: 70%;">
            <input type="text" name="classPath" lay-verify="required" placeholder="请输入类路径" autocomplete="off" id="pathInput"
                   class="layui-input classPathInput">
        </div>
        <button type="button" class="layui-btn layui-btn-sm" id="getSetterFields" style=" margin-top: 1%;">获取属性
        </button>
    </div>
    <div class="fieldattr" style="display: none;">
        <div class="field" style=" height: 38px;line-height: 38px;text-align: center;">
            字段属性
        </div>

    </div>
    <div style="width: 100%;">
        <table class="layui-hide" id="sourceDefTable" lay-filter="sourceDefTable"></table>
    </div>
    <input type="hidden" name="id" placeholder="" autocomplete="off" class="layui-input">
    <div class="layui-btn-container" style="position: absolute;left:41%;bottom:0">
        <button class="layui-btn" type="button" lay-submit lay-filter="dataSourceDefForm">保存</button>
        <button class="layui-btn layui-btn-danger"  type="button" id="return">取消</button>
    </div>
    </div>
</form>
<script type="text/html" id="rowOperation">
    <!--<a class="layui-btn layui-btn-xs" lay-event="moveup">上移</a>-->
    <!--<a class="layui-btn layui-btn-xs" lay-event="movedown">下移</a>-->
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="gradetyl">
    <select class="layui-yonyou-select" lay-ignore name="baseAttr" lay-filter="baseAttr" id="{{d.name}}">
        <option value=""></option>
        <option value="false">否</option>
        <option value="true">是</option>
    </select>
</script>
<script src="${ctx}/public/component.js" type="text/javascript"></script>
<script src="${ctx}/layui/layui.js"></script>
<script>

    var setterFields;
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','form', 'layedit', 'laydate', 'jquery', 'table','common'], function () {
        var form = layui.form, layer = layui.layer, layedit = layui.layedit, laydate = layui.laydate,table = layui.table,setter = layui.setter,common = layui.common;
        var id = '${defId}';
        var $ = layui.$;
        $(".ItemsTips").mouseover(function () {
            layer.tips('格式:classPath|method，例：org.logicalcobwebs.pro，xool.ProxoolFacade|shutDown。', this,
            {
            tips: [1],
            area: ['370px', 'auto']
            });
        });
        table.render({
            elem: '#sourceDefTable'
            , cellMinWidth: 80
            , data: [],
            cols: [[
                {field: 'name', title: '名称',align:'center'}
                , {field: 'comment', title: '描述', edit: 'text',align:'center'}
                , {field: 'type', title: '参数类型',align:'center'}
                , {
                    field: 'baseAttr', title: '是否必填',align:'center',
                    templet: '#gradetyl'
                }
                , {field: 'right', title: '管理', toolbar: "#rowOperation", align: 'center'}
            ]]
        })
        /*新增时获取属性点击事件*/
        $('#getSetterFields').click(function () {
            if(document.getElementById("pathInput").value==""){
                layer.msg('请输入类路径');
            }else{
                $(".fieldattr").css("display", 'block')
                var classPath = $("input[name='classPath']").val();
                table.render({
                    elem: '#sourceDefTable',
                    url: serverPath.systemPath + setter.sysLinkPath + "dataSourceDef/getSetterFields?classPath=" + classPath,
                    height:'full-370',
                    cols: [[
                        {field: 'name', title: '名称'}
                        , {field: 'comment', title: '描述', edit: 'text'}
                        , {field: 'type', title: '参数类型'}
                        , {
                            field: 'baseAttr', title: '是否必填',
                            templet: '#gradetyl'
                        }
                        , {field: 'right', title: '管理', toolbar: "#rowOperation", align: 'center',width:160}
                    ]], done: function (data) {
                        setterFields = data;
                    }
                });
            }
        });
        //关闭弹窗方法
        var index = parent.layer.getFrameIndex(window.name);
        $('#return').click(function () {
            parent.layer.close(index);
        })
        //删除某条属性后最终的属性
        var newSetterArray = [];
        table.on('tool(sourceDefTable)', function (obj) {
            newSetterArray = [];
            data_tr=$(this);
            switch (obj.event) {
                case 'del':
                    layer.confirm('确定删除当前数据吗？', {
                        btn: ['确定', '取消'] //按钮
                    }, function (index) {
                        var delObj = obj.data;
                        obj.del();
                        layer.close(index);
                        var setterArray = setterFields.data;
                        for (var i = 0; i < setterArray.length; i++) {
                            if (delObj.name != setterArray[i].name) {
                                if (setterArray[i].name) {
                                    setterArray[i].baseAttr = $("#" + setterArray[i].name).val();
                                    var param = {};
                                    for (val in setterArray[i]) {
                                        if (val !== 'LAY_TABLE_INDEX') {
                                            param[val] = setterArray[i][val]
                                        }
                                    }
                                    newSetterArray.push(param);
                                }
                            }
                        }
                    });
                    break;
                case 'moveup':
                    var tr = $(data_tr).parent().parent().parent();
                    $(tr).insertBefore($(tr).prev());
                    break;
                case 'movedown':
                    var tr = $(data_tr).parent().parent().parent();
                    $(tr).insertAfter($(tr).next());
                    break;
                    if (obj.event === 'del') {
                        layer.confirm('确定删除当前数据吗？', {
                            btn: ['确定', '取消'] //按钮
                        }, function (index) {
                            var delObj = obj.data;
                            obj.del();
                            layer.close(index);
                            var setterArray = setterFields.data;
                            for (var i = 0; i < setterArray.length; i++) {
                                if (delObj.name != setterArray[i].name) {
                                    if (setterArray[i].name) {
                                        setterArray[i].baseAttr = $("#" + setterArray[i].name).val();
                                        var param = {};
                                        for (val in setterArray[i]) {
                                            if (val !== 'LAY_TABLE_INDEX') {
                                                param[val] = setterArray[i][val]
                                            }
                                        }
                                        newSetterArray.push(param);
                                    }
                                }
                            }
                        })
                    } else if (obj.event === 'moveup') {
                        var tr = $(data_tr).parent().parent().parent();
                        $(tr).insertBefore($(tr).prev());
                    } else if (obj.event === 'movedown') {
                        var tr = $(data_tr).parent().parent().parent();
                        $(tr).insertAfter($(tr).next());
                    }
            }
        });

        function processAttr() {
            var setterArray = setterFields.data;
            var newSetterArray = [];
            for (var i = 0; i < setterArray.length; i++) {
                setterArray[i].baseAttr = $("#" + setterArray[i].name).val();
                var param = {};
                for (val in setterArray[i]) {
                    if(val !== 'LAY_TABLE_INDEX'){
                        param[val] = setterArray[i][val]
                    }
                }
                newSetterArray.push(param);
            }
            return newSetterArray;
        }
        var oid=id;
        /*当是编辑页时执行*/
        if(oid){
            $.ajax({
                url: serverPath.systemPath + setter.sysLinkPath + 'dataSourceDef/getSysDataSourceDef',
                dataType: 'json',
                data: {"id":oid},
                success: function (data) {
                    //表单初始赋值
                    var returnDate = data.data;
                    form.val('formData', {
                        "id": returnDate.id + "",
                        "name": returnDate.name,
                        "initMethod": returnDate.initMethod,
                        "closeMethod": returnDate.closeMethod,
                        "classPath": returnDate.classPath,
                        "system": returnDate.system
                    })
                    var settingJson = returnDate.settingJson;
                    var objData = JSON.parse(settingJson);
                    //表格渲染数据
                    table.render({
                        elem: '#sourceDefTable',
                        data:objData,
                        height:'full-350',
                        cols: [[
                            {field: 'name', title: '名称',align:'center',width:180}
                            , {field: 'comment', title: '描述', edit: 'text',align:'center'}
                            , {field: 'type', title: '参数类型',align:'center'}
                            , {
                                field: 'baseAttr', title: '是否必填',align:'center',
                                templet: '#gradetyl'
                            }
                        ]]
                            , done: function () {
                            table.resize()
                        }
                        ,page:false
                        ,limit:10000
                        , done: function (data) {
                            setterFields = data;
                            for (var i = 0; i < objData.length; i++) {
                                $("#" + objData[i].name).val(objData[i].baseAttr);
                            }
                            if(oid){
                                $("#getSetterFields").css("display","none");
                            }
                        }
                    });
                }
            });
        }

        //监听提交
        form.on('submit(dataSourceDefForm)', function (data) {
            var flag = validClassPath();
            if (flag) {
                if (newSetterArray.length <= 0) {
                    if(setterFields===undefined){
                        layer.alert("请获取属性！",{skin: 'btn-class'});
                        return false;
                    }
                    newSetterArray = processAttr();
                }
                data.field["settingJson"] = JSON.stringify(newSetterArray);
                $.ajax({
                    url: serverPath.systemPath + setter.sysLinkPath + "dataSourceDef/add",
                    type: "POST",
                    contentType: 'application/json',
                    dataType: 'json',
                    data: JSON.stringify(data.field),
                    success: function (result) {
                        if (common.filterRecord(result)) {
                            layer.alert(result.msg, function (index) {
                               layer.close(index);
                                parent.layui.table.reload('sourceDef');
                                parent.layui.totalFilter.init('sourceDef');
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        } else {
                            layer.alert(result.msg,{skin: 'btn-class'});
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.alert("保存失败！" + XMLHttpRequest.status);
                    }
                });
                return false;
            }
        });

        function validClassPath() {
            var validFlag = false;
            var classPath = $("input[name='classPath']").val();
            var param = {"classPath": classPath};
            $.ajax({
                url: serverPath.systemPath + setter.sysLinkPath + "dataSourceDef/validClass",
                dataType: 'json',
                data: param,
                async: false,
                success: function (result) {
                    if (common.filterRecord(result)) {
                        validFlag = true;
                    } else {
                       layer.alert(result.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("验证失败！" + XMLHttpRequest.status);
                }
            });
            return validFlag;
        }
    })
</script>
<style>
    .layui-table-body{
        overflow-x: hidden;
    }
</style>
</body>
</html>